package base;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class Test {
    static ObjectMapper objectMapper = new ObjectMapper();
    static {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    }

    public static Integer test(){
        Integer integer = null;
        return integer.hashCode();
    }
    public static void main(String[] args) throws IOException {
        Float.parseFloat("1.0");
    }

    public static void test2() {
        String json = "{\"user_id\":1,\"user_name\":\"John Doe\",\"email_address\":\"john.doe@example.com\", \"meta\":{\"a\": 10}}";

        test3();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            User user = objectMapper.readValue(json, User.class);
            System.out.println("base.User ID: " + user.userId);
            System.out.println("base.User Name: " + user.userName);
            System.out.println("Email Address: " + user.emailAddress);
            System.out.println("Email Address: " + user.meta.a);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void test1() {
        User user = new User(1, "John Doe", "john.doe@example.com", "secret");

        user.getUserId();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String json = objectMapper.writeValueAsString(user);
            System.out.println(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void test3() {
        User user = new User();
        System.out.println(user.getUserId());
    }

    public static void test5() throws IOException {
        List<Map.Entry<String, String>> a = List.of(Map.entry("a", "b"));
        var b = toJson(a);
        System.out.println(b);
    }

    public static void test6() throws IOException {
        String jsonString = "{\"key1\":123, \"key2\":true, \"key3\":\"value3\"}";
        Map<String, String> a = objectMapper.readValue(jsonString, new TypeReference<Map<String, String>>() {});
        System.out.println(a.get("key1"));
    }

    public static void test4() throws IOException {
        User user = new User(1, "John Doe", "john.doe@example.com", "secret");
        Map<String, Object> test = beanToMap(user);
        System.out.println(test.get("user_id"));
    }

    public static String toJson(Object object) throws IOException {
        return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
    }

    public static Map<String, Object> beanToMap(Object object) throws IOException {
        String json = toJson(object);
        System.out.println(json);
        return objectMapper.readValue(json, new TypeReference<>() {});
    }
}
